package com.qiall.ali;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Map;

public class Sms {
    // 初始化ascClient需要的几个参数
    private static String product = "Dysmsapi";// 短信API产品名称
    private static String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名

    // 发送验证码
    public static void sendTemplate(String phone,
                                    Map<String, String> data,
                                    String templateCode,
                                    String accessKeyId,
                                    String accessKeySecret,
                                    String signName) throws ClientException {

        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
                domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phone);
        request.setSignName(signName);
        // 短信模板
        request.setTemplateCode(templateCode);
        request.setTemplateParam(JSON.toJSONString(data));
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse.getCode() != null
                && sendSmsResponse.getCode().equals("OK")) {
        }

    }
}
